using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Collections;

namespace NWT
{
	public partial class SettingsWindow : Form
	{
		/// <summary>
		/// Configuration to config
		/// </summary>
		protected Configuration configuration = null;

		/// <summary>
		/// Initial form heigh
		/// </summary>
		protected int initialHeight = 0;

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="configuration"></param>
		public SettingsWindow(Configuration configuration)
		{
			this.configuration = configuration;
			InitializeComponent();
			// Add com port names
			AddPortNamesToCombo();
		}

		/// <summary>
		/// Constructor
		/// </summary>
		public SettingsWindow()
		{
			InitializeComponent();
			// Add com port names
			AddPortNamesToCombo();
		}

		/// <summary>
		/// Add com port names
		/// </summary>
		private void AddPortNamesToCombo()
		{
			// get port names
			string[] ports = SerialPort.GetPortNames();
			// sort ports by name
			ArrayList ar = new ArrayList(ports);
			ar.Sort(new NWT.GPSDevice.ComPortStringComparer());
			ports = (string[])ar.ToArray(typeof(string));

			cmbGPSPort.Items.AddRange(ports);
		}

		/// <summary>
		/// Save configuration
		/// </summary>
		protected void SaveConfiguration()
		{
			if (configuration != null)
			{
				// page 1
				configuration.LogToFile = chkLogToFile.Checked;
				//configuration.UseMD5ForPassword = chkPasswordEncryption.Checked;

				// page 2
				configuration.DeviceParameters.PortName = cmbGPSPort.Text;
				configuration.DeviceParameters.BaudRate = Convert.ToInt32(txtGPSRate.Text);
				configuration.DeviceParameters.TimeOut = Convert.ToInt32(txtGPSTimeout.Text);

				// page 3
				configuration.NetworkParameters.URLPath = txtNetURL.Text;
				configuration.NetworkParameters.MarkerName = txtNetName.Text;
				configuration.NetworkParameters.GameName = txtNetGame.Text;
				configuration.NetworkParameters.UserLogin = txtNetUser.Text;
				configuration.NetworkParameters.UserPassword = txtNetPassword.Text;
				configuration.NetworkParameters.ProxyAddress = txtNetProxyAddress.Text;
				configuration.NetworkParameters.ProxyPort = Convert.ToInt32(txtNetProxyPort.Text);
				configuration.NetworkParameters.ProxyLogin = txtNetProxyLogin.Text;
				configuration.NetworkParameters.ProxyPassword = txtNetProxyPassword.Text;
				configuration.NetworkParameters.TimeOut = Convert.ToInt32(txtNetTimeout.Text);
				configuration.NetworkParameters.SendingPause = Convert.ToInt32(txtNetSendingPause.Text);
				configuration.NetworkParameters.MaxSendingPause = Convert.ToInt32(txtNetMaxSendingPause.Text);
				configuration.NetworkParameters.DistanceInMeters = Convert.ToInt32(txtNetDistanceInMeters.Text);

				// page 4
				configuration.TrafficCalculate = chkTrafficCalculate.Checked;
			}
		}

		/// <summary>
		/// Process form loading
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SettingsWindow_Load(object sender, EventArgs e)
		{
			initialHeight = pnlTop.Height;
			if (configuration != null)
			{
				// page 1
				chkLogToFile.Checked = configuration.LogToFile;
				//chkPasswordEncryption.Checked = configuration.UseMD5ForPassword;

				// page 2
				cmbGPSPort.Text = configuration.DeviceParameters.PortName;
				txtGPSRate.Text = configuration.DeviceParameters.BaudRate.ToString();
				txtGPSTimeout.Text = configuration.DeviceParameters.TimeOut.ToString();

				// page 3
				txtNetURL.Text = configuration.NetworkParameters.URLPath;
				txtNetName.Text = configuration.NetworkParameters.MarkerName;
				txtNetGame.Text = configuration.NetworkParameters.GameName;
				txtNetUser.Text = configuration.NetworkParameters.UserLogin;
				txtNetPassword.Text = configuration.NetworkParameters.UserPassword;
				txtNetProxyAddress.Text = configuration.NetworkParameters.ProxyAddress;
				txtNetProxyPort.Text = configuration.NetworkParameters.ProxyPort.ToString();
				txtNetProxyLogin.Text = configuration.NetworkParameters.ProxyLogin;
				txtNetProxyPassword.Text = configuration.NetworkParameters.ProxyPassword;
				txtNetTimeout.Text = configuration.NetworkParameters.TimeOut.ToString();
				txtNetSendingPause.Text = configuration.NetworkParameters.SendingPause.ToString();
				txtNetMaxSendingPause.Text = configuration.NetworkParameters.MaxSendingPause.ToString();
				txtNetDistanceInMeters.Text = configuration.NetworkParameters.DistanceInMeters.ToString();

				// page 4
				chkTrafficCalculate.Checked = configuration.TrafficCalculate;
			}
		}

		/// <summary>
		/// Perform button event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void mniSave_Click(object sender, EventArgs e)
		{
			try
			{
				SaveConfiguration();
				this.DialogResult = DialogResult.OK;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
				this.DialogResult = DialogResult.None;
			}
		}

		/// <summary>
		/// Perform button event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void mniCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
		}

		/// <summary>
		/// Proecess event
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SettingsWindow_Closed(object sender, EventArgs e)
		{
		}

		/// <summary>
		/// Reset traffic
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnClearTraffic_Click(object sender, EventArgs e)
		{
			GPSManager.NetworkTrafficCounter.Reset();
		}
	}
}